<?php
/*
 * Aclass_info 信息类
 * 2010-07-08
 * http://www.abans.cn		abans@qq.com
 */
class Aclass_info{
	function  __construct($dbname) {
		if(empty($dbname)){
			$dbname = Acan_table_info;
		}
		$this->dbname = $dbname;
		$this->info = Afun_db()->model($dbname);
	}
	function cache_name($name){
		return $this->dbname.'-'.$name;
	}

	function update($name,$data,$rid){
		if(!is_array($data)){return true;}
		$model = Afunc_model()->model($name);//获取模型数据
		$columns = $model->columns;
		foreach($data as $k=>$v){
			unset($con);
			$con['model_id'] = $model->model_id;
			$con['rid'] = $rid;
			$con['type_id'] = $columns[$k];
			$rs = $this->info->count($con);
			if($rs == '0'){
				if(empty($v)){continue;}
				$con['data'] = $v;
				$this->info->save($con);
			}elseif($rs == '1'){
				$this->info->update("`data` = '$v'",$con);
			}else{
				$this->info->delete($con);
				$con['data'] = $v;
				$this->info->save($con);
			}
		}
		return true;
	}

	function data($name,$id,$type=""){
		$model = Afunc_model()->model($name);//获取模型数据
		$models = $this->model($model->model_id,$id);
		foreach($models as $val){
			$drr[$val['type_id']] = $val['data'];
		}
		if($type=="all"){
			foreach($model->data as $key=>$val){
				$column = $val->name;
				$data->info->$column = $drr[$key];
				$data->html->$column = $this->html($drr[$key],$val);
			}
			$data->model = $model;
		}else{
			foreach($model->data as $key=>$val){
				$column = $val->name;
				$data->$column = $drr[$key];
			}
			$data->id = $id;
		}
		return $data;
	}
	//查询单条记录的所有信息
	function model($model_id,$rid){
		$data = $this->info->findAll("model_id = '$model_id' and rid = '$rid'");
		return $data;
	}
	//查询所有记录的信息
	function models($name,$where=""){
		$model = Afunc_model()->model($name);//获取模型数据
		$con->where = "model_id = '$model->model_id'";
		if ($where) {
			if (is_array($where->is)) {//等于
				foreach ($where->is as $key => $val) {
					$con->where .= " and (type_id=".$model->columns[$key]." and data = '".$val."')";
				}
			}
			if (is_array($where->notis)) {//不等于
				foreach ($where->notis as $key => $val) {
					$con->where .= " and (type_id=".$model->columns[$key]." and data != '".$val."')";
				}
			}
			if (is_array($where->like)) {//LIKE
				foreach ($where->like as $key => $val) {
					$con->where .= " and (type_id=".$model->columns[$key]." and data LIKE '".$val."')";
				}
			}
			if (is_array($where->small)) {//小于
				foreach ($where->small as $key => $val) {
					$con->where .= " and (type_id=".$model->columns[$key]." and data < '".$val."')";
				}
			}
			if (is_array($where->big)) {//大于
				foreach ($where->big as $key => $val) {
					$con->where .= " and (type_id=".$model->columns[$key]." and data > '".$val."')";
				}
			}
			if (is_array($where->in)) {//包含
				foreach ($where->in as $key => $val) {
					$con->where .= " and (type_id=".$model->columns[$key]." and data in (".$val."))";
				}
			}
			if (is_array($where->notin)) {//不包含
				foreach ($where->notin as $key => $val) {
					$con->where .= " and (type_id=".$model->columns[$key]." and data not in (".$val."))";
				}
			}
		}
		
		if($search){
			$con->where .= " and data LIKE '$search'";
		}
		$con->group = "a.rid";
		$models = $this->info->findAll($con);
		foreach ($models as $key => $val) {
			$data[] = $this->data($name,$val['rid']);
		}
		return $data;
	}
	//生成html代码
	function html($value,$data){
		$html = Afun_acan_new("html");
		$arr['type'] = $data->type;
		$arr['id'] = 'saveinfo_'.$data->name;
		$arr['name'] = 'saveinfo['.$data->name.']';
		if($data->type == 'text'){
			$arr['value'] = $value;
			return $html->tag('input',$arr);
		}elseif($data->type == 'textarea'){
			return $html->tag('textarea',$arr,$value);
		}elseif($data->type == 'radio'){
			return $html->inputs($arr,$data->data,$value,$data->label);
		}elseif($data->type == 'checkbox'){
			$arr['name'] = 'saveinfo['.$data->name.'][]';
			return $html->inputs($arr,$data->data,$value,$data->label);
		}elseif($data->type == 'select'){
			return $html->select($arr,$data->data,$value,$data->label);
		}
	}
	
	
	
	
	
	
	//获取模型的所有类型
	function type($name){
		$models = $this->type->find("pid = '0' and name = '$name'");
		$pid = $models['id'];
		$types = $this->type->findAll("pid = '$pid'");
		if(is_array($types))
		foreach($types as $k=>$v){
			$arr[$v['type']] = $v['id'];
		}
		$data['types'] = $arr;
		$data['model_id'] = $pid;
		return $data;
	}


}

?>
